/**
 * 빈 여부
 * @returns {Boolean}
 */
String.prototype.isEmpty = function() {
	return null == this || 0 == this.trim().length;
};
/**
 * 숫자형 변환 여부
 * @returns {Boolean}
 */
String.prototype.isNumber = function() {
	return this != null && /\d/.test(this) && !isNaN(this);
};
/**
 * 공백 삭제
 * @returns
 */
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
/**
 * 특정 문자 치환
 * @param searchStr		찾을문자
 * @param replaceStr		바뀔문자
 * @returns
 */
String.prototype.replaceAll = function(searchStr, replaceStr) {
	return this.split(searchStr).join(replaceStr);
};
/**
 * 왼쪽 특정문자채움
 * @param size			채울사이즈
 * @param padChar	채울문자
 * @returns
 */
String.prototype.leftPad = function(size, padChar) {
	var tmpStr = this.split('');
	for (var i = 0, lenSize = size - this.length; i < lenSize; i++) {
		tmpStr.unshift(padChar);
	}
	return tmpStr.join('');
};
/**
 * 오른쪽 특정문자채움
* @param size			채울사이즈
 * @param padChar	채울문자
 * @returns
 */
String.prototype.rightPad = function(size, padChar) {
	var tmpStr = this.split('');
	for (var i = 0, lenSize = size - this.length; i < lenSize; i++) {
		tmpStr.push(padChar);
	}
	return tmpStr.join('');
};

/**
 * 숫자만 남기고 삭제
 * @returns
 */
String.prototype.toCharNum = function() {
	return this.replace(/[^\d]/g, '');
};

/**
 * 천단위 콤마 추가
 * @returns
 */
String.prototype.toComma = function() {
	if (!this.isNumber()) return this;
	var val = this.toCharNum();
	var reg = /(^[+-]?\d+)(\d{3})/;
	while (reg.test(val)) val = val.replace(reg, '$1' + ',' + '$2');
	return val;
};

/**
 * 데이트타입 변경 여부
 * @returns {Boolean}
 */
String.prototype.isDate = function() {
	if (this.toCharNum().isEmpty()) return false;
	var tmpValue = this.toCharNum();
	var reg = null;
	if (6 == tmpValue.length) {
		reg = /^(\d{4})(\d{2})$/;
	} else if (8 == tmpValue.length) {
		reg = /^(\d{4})(\d{2})(\d{2})$/;
	} else if (10 == tmpValue.length) {
		reg = /^(\d{4})(\d{2})(\d{2})(\d{2})$/;
	} else if (12 == tmpValue.length) {
		reg = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})$/;
	} else if (14 == tmpValue.length) {
		reg = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/;
	} else {
		return false;
	}
	var matchs = reg.exec(tmpValue);
	if (1 > matchs[2] || 12 < matchs[2]) {
		return false;
	} else if (2 < matchs.length && (1 > matchs[3] || 31 < matchs[3])) {
		return false;
	} else if (2 < matchs.length && -1 < '04,06,09,11'.indexOf(matchs[2])
			&& 30 < matchs[3]) {
		return false;
	} else if (2 < matchs.length && 2 == matchs[2]) {
		if (matchs[2].isLeapMonth() && 29 != matchs[3]) return false;
		else if (28 != matchs[3]) return false;
	} else if (4 < matchs.length && 23 < matchs[4]) {
		return false;
	} else if ((5 < matchs.length || 6 < matchs.length) && (59 < matchs[5] || 59 < matchs[6])) {
		return false;
	}
	return true;
};
/**
 * String Date 변경
 * @returns {Date}
 */
String.prototype.toDate = function() {
	if (!this.isDate()) throw new Error('잘못된 날짜입니다.');
	var tmpValue = this.toCharNum();
	var reg = null;
	var matchs = null;
	if (6 == tmpValue.length) {
		reg = /^(\d{4})(\d{2})$/;
		matchs = reg.exec(tmpValue);
		return new Date(matchs[1], matchs[2] - 1, 1);
	} else if (8 == tmpValue.length) {
		reg = /^(\d{4})(\d{2})(\d{2})$/;
		matchs = reg.exec(tmpValue);
		return new Date(matchs[1], matchs[2] - 1, matchs[3]);
	} else if (10 == tmpValue.length) {
		reg = /^(\d{4})(\d{2})(\d{2})(\d{2})$/;
		matchs = reg.exec(tmpValue);
		return new Date(matchs[1], matchs[2] - 1, matchs[3], matchs[4]);
	} else if (12 == tmpValue.length) {
		reg = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})$/;
		matchs = reg.exec(tmpValue);
		return new Date(matchs[1], matchs[2] - 1, matchs[3], matchs[4],
				matchs[4], 0);
	} else if (14 == tmpValue.length) {
		reg = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/;
		matchs = reg.exec(tmpValue);
		return new Date(matchs[1], matchs[2] - 1, matchs[3], matchs[4],
				matchs[5], matchs[6]);
	}
	return new Date();
};
/**
 * Byte 길이 반환
 * @returns {Number}
 */
String.prototype.byteLength = function() {
	var len = 0, c = null;
	for (var idx = 0; idx < this.length; idx++) {
		c = escape(this.charAt(idx));
		if (c.length == 1) {
			len++;
		} else if (-1 != c.indexOf("%u")) {
			len += 2;
		} else if (-1 != c.indexOf("%")) {
			len += c.length / 3;
		}
	}
	return len;
};

/**
 * 금액 한글로 반환(숫자 20자리까지만 가능)
 * @returns {String}
 */
String.prototype.toAmtHangul = function() {
	if (!this.isNumber()) throw new Error('숫자가 아닙니다.');
	if(20 < this.toCharNum().length) throw new Error('사용할 수 있는 범위를 초과하였습니다. 20자리까지 가능합니다.');
	var arrNum = new Array('영', '일', '이', '삼', '사', '오', '육', '칠', '팔', '구');
	var arrUnit = new Array('천', '백', '십', '경', '천', '백', '십', '조', '천', '백', '십', '억', '천', '백', '십', '만', '천', '백', '십', '');
	var isComma = false;
	var won = new String();
	var num = this.toCharNum().leftPad(20, "0").split('');
	for (var i = 0, size = num.length; i < size; i++) {
		if (0 < num[i]) {
			won += arrNum[parseInt(num[i])] + arrUnit[i];
			if (3 == i || 7 == i || 11 == i || 15 == i || 19 == i) {
				isComma = false;
			} else {
				isComma = true;
			}
		} else {
			if (isComma && (3 == i || 7 == i || 11 == i || 15 == i || 19 == i)) {
				won += arrUnit[i];
				isComma = false;
			}
		}
	}
	return won;
};

/***
 * 윤달 체크
 * @returns {Boolean}
 */
String.prototype.isLeapMonth = function() {
	if ((this % 4 == 0 && this % 100 != 0) || this % 400 == 0) return true;
	else return false;
};
/***
 * 윤달 체크
 * @returns {Boolean}
 */
Number.prototype.isLeapMonth = function() {
	if ((this % 4 == 0 && this % 100 != 0) || this % 400 == 0) return true;
	else return false;
};
/**
 * Date 포맷 반환
 * @returns {String}
 */
Date.prototype.format = function(format) {
	if (!this.valueOf()) return '';
	var date = this;
	var weekName = [ "일요일", "월요일", "화요일", "수요일", "목요일", "금요일", "토요일" ];
	var week = [ "일", "월", "화", "수", "목", "금", "토" ];
	return format.replace(/(yyyy|yy|MM|dd|W|w|hh|mm|ss|ap|AP)/gi, function($1) {
		switch ($1) {
		case "yyyy":
			return date.getFullYear();
		case "yy":
			return (date.getFullYear() % 1000).toString().leftPad(2, '0');
		case "MM":
			return (date.getMonth() + 1).toString().leftPad(2, '0');
		case "dd":
			return date.getDate().toString().leftPad(2, '0');
		case "W":
			return weekName[date.getDay()];
		case "w":
			return week[date.getDay()];
		case "HH":
			return date.getHours().toString().leftPad(2, '0');
		case "hh":
			return ((h = date.getHours() % 12) ? h : 12).toString().leftPad(2, '0');
		case "mm":
			return date.getMinutes().toString().leftPad(2, '0');
		case "ss":
			return date.getSeconds().toString().leftPad(2, '0');
		case "ap":
			return date.getHours() < 12 ? "오전" : "오후";
		case "AP":
			return date.getHours() < 12 ? "AM" : "PM";
		default:
			return $1;
		}
	});
};